<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>03-数据存储原理</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-4">
                            <a href="#python数据存储原理">python数据存储原理</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-1">
                            <a href="#标识符">标识符</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#命名空间">命名空间</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#作用域">作用域</a>
                        </li>
                    
                
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#LEGB-命名空间">LEGB 命名空间</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#LEGB-访问规则">LEGB 访问规则</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#global、nonlocal、locals()、globals()">global、nonlocal、locals()、globals()</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#理解a-=-a-+-1">理解a = a + 1</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#上">上</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#中">中</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#右">右</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#左">左</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#可变对象的引用(除了数字和字符串)">可变对象的引用(除了数字和字符串)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#解析">解析</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#可变对象的浅拷贝">可变对象的浅拷贝</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#修改字典的列表">修改字典的列表</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#导入copy模块实现deep-copy">导入copy模块实现deep copy</a>
                        </li>
                    
                
            
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="kn">from</span> <span class="nn">IPython.display</span> <span class="k">import</span> <span class="n">display</span>              <span class="c1"># 美化输出数组</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>  <span class="c1"># 打印所有单行变量</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="python&#25968;&#25454;&#23384;&#20648;&#21407;&#29702;">python&#25968;&#25454;&#23384;&#20648;&#21407;&#29702;<a class="anchor-link" href="#python&#25968;&#25454;&#23384;&#20648;&#21407;&#29702;">&#182;</a></h4><p><strong>变量 引用 对象</strong></p>

<pre><code>x = 1

</code></pre>
<p>内存中只创建了对象<code>1</code>，变量x只是贴在对象<code>1</code>的标签，<code>=</code>是赋值即引用<br>
在python中一切皆对象，里面又有不可变对象和可变对象</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>  <span class="c1"># 内存中创建了一个对象5</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">b</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[19]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>5</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="nb">id</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>  <span class="c1"># 一个对象贴了两个标签</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[20]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>94251520061376</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[20]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>94251520061376</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">b</span> <span class="o">=</span> <span class="mi">3</span>  <span class="c1"># 内存中新建了一个对象3</span>
<span class="nb">id</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>  <span class="c1"># 对象引用换成了b</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[21]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>94251520061312</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>上例 a, b 共享了同一个 ID、同一个值、同一个类型。因此 a, b 表达的是同一个对象，但 a, b 又明显是不同的，比如一个叫 'a'，一个叫 'b'...既然是同一个对象，为什么又有不同的名字呢？难道名字不是对象的属性？</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="&#26631;&#35782;&#31526;">&#26631;&#35782;&#31526;<a class="anchor-link" href="#&#26631;&#35782;&#31526;">&#182;</a></h1><p>事实确实如此，这是 Python 比较特殊一点：如同'a' 'b' 这样的名称其实有一个共同的名字：identifier（注意不要与 ID 混淆了），中文名为“标识符”，来解释一下：</p>
<p>标识符：各类对象的名称，比如函数名、方法名、类名，变量名、常量名等。</p>
<p>在 Python 中赋值并不会直接复制数据，而只是将名称绑定到对象，对象本身是不知道也不需要关心（该关心这个的是程序猿）自己叫什么名字的。一个对象甚至可以指向不同的标识符，上例中的'a' 'b'便是如此。真正管理这些名子的事物叫“命名空间”。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#21629;&#21517;&#31354;&#38388;">&#21629;&#21517;&#31354;&#38388;<a class="anchor-link" href="#&#21629;&#21517;&#31354;&#38388;">&#182;</a></h3><p>命名空间（Namespace）：名字（标识符）到对象的映射。</p>
<p>简而言之，命名空间可以理解为：记录对象和对象名字对应关系的空间；现今 Python 的大部分命名空间是通过字典来实现的，也即一个命名空间就是名字到对象的映射，标识符是键，对象则是值。</p>
<h3 id="&#20316;&#29992;&#22495;">&#20316;&#29992;&#22495;<a class="anchor-link" href="#&#20316;&#29992;&#22495;">&#182;</a></h3><p>与命名空间相对的一个概念就是“作用域”，那么什么又是作用域呢？</p>
<p>作用域（Scope）：本质是一块文本区域，Python 通过该文本区域可以直接访问相应的命名空间。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="LEGB-&#21629;&#21517;&#31354;&#38388;">LEGB &#21629;&#21517;&#31354;&#38388;<a class="anchor-link" href="#LEGB-&#21629;&#21517;&#31354;&#38388;">&#182;</a></h3><p>这四类命名空间可以简记为 LEGB:</p>
<p>局部命名空间（local）：指的是一个函数或者一个类所定义的名称空间；包括函数的参数、局部变量、类的属性等。</p>
<p>闭包命名空间（enclosing function）：闭包函数 的名称空间（Python 3 引入）。</p>
<p>全局命名空间（global）：读入一个模块（也即一个.py文档）后产生的名称空间。</p>
<p>内建命名空间（builtin）：Python 解释器启动时自动载入<strong>built</strong>模块后所形成的名称空间；诸如 str/list/dict...等内置对象的名称就处于这里。</p>

<pre><code>scopes = {
    "local": {"locals": None,
             "non-local": {"locals": None,
                          "global": {"locals": None,
                                    "built-in": ["built-ins"]}}},
}</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="LEGB-&#35775;&#38382;&#35268;&#21017;">LEGB &#35775;&#38382;&#35268;&#21017;<a class="anchor-link" href="#LEGB-&#35775;&#38382;&#35268;&#21017;">&#182;</a></h4><p>同样的标识符在各层命名空间中可以被重复使用而不会发生冲突，但 Python 寻找一个标识符的过程总是从当前层开始逐层往上找，直到首次找到这个标识符为止：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="global&#12289;nonlocal&#12289;locals()&#12289;globals()">global&#12289;nonlocal&#12289;locals()&#12289;globals()<a class="anchor-link" href="#global&#12289;nonlocal&#12289;locals()&#12289;globals()">&#182;</a></h3><ul>
<li>global：声明该标识符引用的对象来自于全局变量，但并不能直接在当前作用域创建该标识符</li>
<li>nonlocal：声明该标识符引用的对象来自于父函数变量，并在当前作用域创建与父函数变量同名的标识符</li>
<li>locals(): 返回是当前局部变量的深拷贝，修改locals() 中变量值的时候，实际上对于原变量本身是没有任何影响的。  </li>
<li>globals(): 返回的是全局变量的字典，修改其中的内容，值会真正的发生改变</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[52]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="k">def</span> <span class="nf">do_local</span><span class="p">():</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;local spam&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;do_local_scope:&#39;</span><span class="p">,</span> <span class="nb">locals</span><span class="p">())</span>
    <span class="k">def</span> <span class="nf">do_nonlocal</span><span class="p">():</span>
        <span class="k">nonlocal</span> <span class="n">spam</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;do_nonlocal_scope:&#39;</span><span class="p">,</span> <span class="nb">locals</span><span class="p">())</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;nonlocal spam&quot;</span>
    <span class="k">def</span> <span class="nf">do_global</span><span class="p">():</span>
        <span class="k">global</span> <span class="n">spam</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;do_global_scope:&#39;</span><span class="p">,</span> <span class="nb">locals</span><span class="p">())</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;global spam&quot;</span>
        
    <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;func_scope&quot;</span>
    <span class="n">display</span><span class="p">(</span><span class="nb">locals</span><span class="p">())</span>
    <span class="n">do_local</span><span class="p">()</span>
    <span class="n">do_nonlocal</span><span class="p">()</span>
    <span class="n">do_global</span><span class="p">()</span>

<span class="n">func</span><span class="p">()</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>




<div class="output_text output_subarea ">
<pre>{&#39;do_global&#39;: &lt;function __main__.func.&lt;locals&gt;.do_global&gt;,
 &#39;do_local&#39;: &lt;function __main__.func.&lt;locals&gt;.do_local&gt;,
 &#39;do_nonlocal&#39;: &lt;function __main__.func.&lt;locals&gt;.do_nonlocal&gt;,
 &#39;spam&#39;: &#39;func_scope&#39;}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>do_local_scope: {&#39;spam&#39;: &#39;local spam&#39;}
do_nonlocal_scope: {&#39;spam&#39;: &#39;func_scope&#39;, &#39;b&#39;: 1}
do_global_scope: {}
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="k">def</span> <span class="nf">do_local</span><span class="p">():</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;local spam&quot;</span>
    <span class="k">def</span> <span class="nf">do_nonlocal</span><span class="p">():</span>
        <span class="k">nonlocal</span> <span class="n">spam</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;nonlocal spam&quot;</span>
    <span class="k">def</span> <span class="nf">do_global</span><span class="p">():</span>
        <span class="k">global</span> <span class="n">spam</span>
        <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;global spam&quot;</span>
        
    <span class="n">spam</span> <span class="o">=</span> <span class="s2">&quot;test spam&quot;</span>
    <span class="n">do_local</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;After local assignment:&quot;</span><span class="p">,</span> <span class="n">spam</span><span class="p">)</span>
    
    <span class="n">do_nonlocal</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;After nonlocal assignment:&quot;</span><span class="p">,</span> <span class="n">spam</span><span class="p">)</span>
    
    <span class="n">do_global</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;After global assignment:&quot;</span><span class="p">,</span> <span class="n">spam</span><span class="p">)</span>

<span class="n">func</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;In global scope:&quot;</span><span class="p">,</span> <span class="n">spam</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#29702;&#35299;a-=-a-+-1">&#29702;&#35299;a = a + 1<a class="anchor-link" href="#&#29702;&#35299;a-=-a-+-1">&#182;</a></h3><p>如果在现实中数学去理解，肯定会很困惑。但是在python中，或者是面向对象的编程语言中，一切就说的通，例如：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span>  <span class="c1"># 上→中→右→左</span>

<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[2]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#19978;">&#19978;<a class="anchor-link" href="#&#19978;">&#182;</a></h4><p>上就是<code>a = 1</code>，该赋值发生在全局作用域，a标志符和1在内存中的对象建立联系</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#20013;">&#20013;<a class="anchor-link" href="#&#20013;">&#182;</a></h4><p>中就是<code>=</code>这个赋值操作发生在哪，也是在全局作用域，<code>这一步很关键</code>，确定了后面的操作是在哪进行</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#21491;">&#21491;<a class="anchor-link" href="#&#21491;">&#182;</a></h4><p>因为在上一步<code>中</code>里确定了是全局作用域，那么<code>a + 1</code>就会去全局作用域中找a对应的对象（1在内存中对应的对象）参与计算，结果是2</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#24038;">&#24038;<a class="anchor-link" href="#&#24038;">&#182;</a></h4><p>这一步要做什么是由<code>中</code>这一步决定的，这里是赋值。左边的标志符（a）和上一步<code>右</code>的结果（2在内存中对应的对象）建立联系</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#21487;&#21464;&#23545;&#35937;&#30340;&#24341;&#29992;(&#38500;&#20102;&#25968;&#23383;&#21644;&#23383;&#31526;&#20018;)">&#21487;&#21464;&#23545;&#35937;&#30340;&#24341;&#29992;(&#38500;&#20102;&#25968;&#23383;&#21644;&#23383;&#31526;&#20018;)<a class="anchor-link" href="#&#21487;&#21464;&#23545;&#35937;&#30340;&#24341;&#29992;(&#38500;&#20102;&#25968;&#23383;&#21644;&#23383;&#31526;&#20018;)">&#182;</a></h3><blockquote><p>可以创建多个"一样"的对象，但是id不同</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>  <span class="c1"># --&gt;</span>

<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">[:]</span>
<span class="n">y</span>      <span class="c1"># --&gt;</span>
<span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>  <span class="c1"># --&gt;</span>

<span class="n">z</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="nb">id</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[22]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140201480827080</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[22]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[1, 2, 3]</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[22]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140201480830088</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[22]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140201480813512</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#35299;&#26512;">&#35299;&#26512;<a class="anchor-link" href="#&#35299;&#26512;">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[23]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;a&#39;</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>

<span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>  <span class="c1"># 列表对象</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[23]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(140201480903624, 140201480812360)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>  <span class="c1"># 三个 &#39;a&#39; id一样</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[24]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(140202005359616, 140202005359616, 140202005359616)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[25]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span>  <span class="c1"># 列表的子对象数字1</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[25]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(94251520061248, 94251520061248)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[26]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 修改列表内容</span>
<span class="n">y</span><span class="o">.</span><span class="n">remove</span><span class="p">([</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>

<span class="n">y</span>     <span class="c1"># --&gt;</span>
<span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="c1">#--&gt; 因为是修改原对象，所有id不变</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[26]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;a&#39;, 1]</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[26]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140201480903624</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#21487;&#21464;&#23545;&#35937;&#30340;&#27973;&#25335;&#36125;">&#21487;&#21464;&#23545;&#35937;&#30340;&#27973;&#25335;&#36125;<a class="anchor-link" href="#&#21487;&#21464;&#23545;&#35937;&#30340;&#27973;&#25335;&#36125;">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>  <span class="c1"># 最底层的对象只有1,2,3</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">y</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[27]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: [1, 2, 3]}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[28]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>  <span class="c1"># 父对象id显示不一样</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[28]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(140201480813832, 140201480813256)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">]),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">])</span>  <span class="c1"># 子对象&#39;a&#39;的id一样</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[29]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(94251520061248, 94251520061248)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]),</span> <span class="nb">id</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">])</span>  <span class="c1"># 子对象&#39;b&#39;的id一样</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[30]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(140201480929928, 140201480929928)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>从上面可以看出，浅复制只是创建一个新的父对象，子对象还是同一个对象</p>
<h4 id="&#20462;&#25913;&#23383;&#20856;&#30340;&#21015;&#34920;">&#20462;&#25913;&#23383;&#20856;&#30340;&#21015;&#34920;<a class="anchor-link" href="#&#20462;&#25913;&#23383;&#20856;&#30340;&#21015;&#34920;">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[34]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">y</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

<span class="n">y</span>  <span class="c1"># --&gt;</span>
<span class="n">x</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[34]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: [1, 3]}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[34]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: [1, 3]}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#23548;&#20837;copy&#27169;&#22359;&#23454;&#29616;deep-copy">&#23548;&#20837;copy&#27169;&#22359;&#23454;&#29616;deep copy<a class="anchor-link" href="#&#23548;&#20837;copy&#27169;&#22359;&#23454;&#29616;deep-copy">&#182;</a></h3><blockquote><p>deepcopy可以创建两个id不一样的可变对象，但不影响不可变对象</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>                <span class="c1"># 浅拷贝和前面一样</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>            <span class="c1"># 深拷贝</span>

<span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">])</span>  <span class="c1"># --&gt;</span>
<span class="nb">id</span><span class="p">(</span><span class="n">z</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">])</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140592905522888</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>140592905541640</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">z</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

<span class="n">x</span>  <span class="c1"># --&gt;</span>
<span class="n">z</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[5]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: [1, 2, 3]}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[5]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: [1, 3]}</pre>
</div>

</div>

</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>
